import os
import tos
import  pandas as pd
import json
from datetime import datetime
from data_get import read_upload_100ms,read_can_out

# Access key 和 Secret key 可在用户火山引擎账号中查找
ak = "AKLTMmFmMzVmNmY5ZDEyNDNmNWEyZTU2MDM3Y2EzMDJlZTk"
sk = "WVdNeFpXRXdNekV4Tm1ZME5EZGxPRGc0TUdFNE1HWXdNV1ZqTkdNNU9HRQ=="
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-beijing.volces.com"
region = "cn-beijing"
def get_time_str(secs):
    # 使用datetime.fromtimestamp()将时间戳转换为datetime对象
    dt_object = datetime.fromtimestamp(secs)
    # 使用strftime()方法将datetime对象格式化为字符串
    time_string = dt_object.strftime('%Y-%m-%d %H:%M:%S')
    return time_string
# 读取json文件以字符串形势读取
def readJsonFileToStr(source_dir):
    data_list=[]
    json_list = []
    with open(source_dir, 'r') as file:

        for line in file:
            # 用 json.loads 解析每一行的 JSON 对象
            json_str=line.replace(" ", "").replace("\n", "")

            # and json_str == '}{':

            if json_str is not None and json_str != '}{':
                # print(json_str)
                json_list.append(json_str)
            elif json_str is not None and json_str == '}{':
                json_list.append("}")
                data_list.append(json_list)
                json_list = []
                json_list.append("{")
        data_list.append(json_list)
        return data_list
def write_2_local(source_dir,local_dir,object_key):
    data_list =readJsonFileToStr(source_dir)
    with open(local_dir, 'w') as file:
        for list1 in data_list:
            contnet=""
            for str_js in list1:
                contnet+=str_js
            data = json.loads(contnet)
            data['path'] =object_key
            json_string = json.dumps(data, ensure_ascii=False)
            file.write(json_string)
            file.write('\n')
def path_exsist(folder_path):
    if not os.path.exists(folder_path):
        # 如果文件夹不存在，创建它
        os.makedirs(folder_path)
        print("创建成功！")
    else:
        print("目录已经存在。")
def download(bucket_name,object_key,file_name,localpath):
    print("判断文件是否存在，不存在则创建。。")
    path_exsist(localpath)
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 若 file_name 为目录则将对象下载到此目录下, 文件名为对象名
        client.get_object_to_file(bucket_name, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))


if __name__ == '__main__':
    bucket_name = "hz-data-extract-2100095994-cn-beijing"
    #解析完的路径
    object_key = "EP40/TDA4/SC/v2212489695/20240109/09/fcmea54jc77u76vk9gpd0-EP40_TDA4_drive_sc-20240109-093407-2311.zip/20240109-093357-hz_st_upload-2311/ST_Upload_100ms_t.json"
    vechicle_id=object_key.split("/")[3]
    daystr = object_key.split("/")[4]
    hourstr = object_key.split("/")[5]
    file_prefix= object_key.split("/")[6]
    filename=object_key.split("/")[len(object_key.split("/"))-1]
    print("filename: "+filename)
    if filename=="ST_Upload_100ms_t.json":
        localpath = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix
        file_name = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/" + filename
        download(bucket_name, object_key, file_name, localpath)
        upload_100ms_file = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/ST_Upload_100ms_t.json"
        write_2_local(file_name, upload_100ms_file, object_key)
        dict_100ms_file = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/dict_100ms_t.json"
        read_upload_100ms(upload_100ms_file, dict_100ms_file)
    elif filename == "ST_NOP_CANOut.json":
        localpath = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix
        file_name = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/" + filename
        download(bucket_name, object_key, file_name, localpath)
        upload_canout_file = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/ST_NOP_CANOut.json"
        write_2_local(file_name, upload_canout_file, object_key)
        dict_canout_file = "./test/" + vechicle_id + "/" + daystr + "/" + hourstr + "/" + file_prefix + "/dict_canout_t.json"
        read_can_out(upload_canout_file, dict_canout_file)





